package com.dmtt.web.service.impl;

import com.dmtt.web.entity.GrainRateCount;
import com.dmtt.web.entity.StatisticsAgvTime;
import com.dmtt.web.mapper.CarStatusMapper;
import com.dmtt.web.mapper.StatisticsMapper;
import com.dmtt.web.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private CarStatusMapper carStatusMapper;


    @Override
    public void grainRate(String startTime,String endTime) {
        List<StatisticsAgvTime> list = statisticsMapper.grainRate(startTime,endTime);
        statisticsMapper.deleteTemp();
        for (StatisticsAgvTime statistics : list){
            StatisticsAgvTime tempAgv = new StatisticsAgvTime();
            int countTime = countTime(statistics);
            tempAgv.setAgvName(statistics.getAgvName());
            tempAgv.setStatus(statistics.getStatus());
            tempAgv.setCountTime(countTime);
            tempAgv.setStartTime(startTime.substring(0,10));
            statisticsMapper.saveAgv(tempAgv);
        }
        List<StatisticsAgvTime> tempList = statisticsMapper.tempFind();
        for (StatisticsAgvTime agvTime : tempList) {
            agvTime.setAgvId(UUID.randomUUID().toString().replaceAll("-", ""));
            agvTime.setCurrTime(startTime.substring(0,10));
            statisticsMapper.saveAgvCountTime(agvTime);
        }
    }

    public void grainRate1(String startTime,String endTime) {
        List<StatisticsAgvTime> list = statisticsMapper.grainRate(startTime,endTime);
        statisticsMapper.deleteTemp1();
        for (StatisticsAgvTime statistics : list){
            StatisticsAgvTime tempAgv = new StatisticsAgvTime();
            int countTime = countTime(statistics);
            tempAgv.setAgvName(statistics.getAgvName());
            tempAgv.setStatus(statistics.getStatus());
            tempAgv.setCountTime(countTime);
            tempAgv.setStartTime(startTime.substring(0,10));
            statisticsMapper.saveAgv(tempAgv);
        }
        List<StatisticsAgvTime> tempList = statisticsMapper.tempFind1();
        for (StatisticsAgvTime agvTime : tempList) {
            agvTime.setAgvId(UUID.randomUUID().toString().replaceAll("-", ""));
            agvTime.setCurrTime(startTime.substring(0,10));
            statisticsMapper.saveAgvCountTime1(agvTime);
        }
    }

//    @Override
//    public void grainRateCount(String startTime,String endTime) {
//        List<GrainRateCount> list = statisticsMapper.grainRateCount(startTime,endTime);
//        for (GrainRateCount object : list) {
//            object.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
//            object.setDayTime(startTime.substring(0,10));
//            statisticsMapper.saveGrainCount(object);
//        }
//    }

    private int countTime(StatisticsAgvTime statistics) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1;
        Date date2;
        try {
            date1 = formatter.parse(statistics.getStartTime());
            date2 = formatter.parse(statistics.getEndTime());
            int l = (int) ((date2.getTime() - date1.getTime()) / (1000));
            return l;
        } catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void currDayGrainCount(String startTime,String endTime){
        List<StatisticsAgvTime> list = statisticsMapper.grainRate(startTime,endTime);
        statisticsMapper.deleteTemp1();
        carStatusMapper.deleteStatisticeTemp();
        for (StatisticsAgvTime statistics : list){
            StatisticsAgvTime tempAgv = new StatisticsAgvTime();
            int countTime = countTime(statistics);
            tempAgv.setAgvName(statistics.getAgvName());
            tempAgv.setStatus(statistics.getStatus());
            tempAgv.setCountTime(countTime);
            tempAgv.setStartTime(startTime.substring(0,10));
            statisticsMapper.saveAgv1(tempAgv);
        }
        List<StatisticsAgvTime> tempList = statisticsMapper.tempFind1();
        for (StatisticsAgvTime agvTime : tempList) {
            agvTime.setAgvId(UUID.randomUUID().toString().replaceAll("-", ""));
            agvTime.setCurrTime(startTime.substring(0,10));
            statisticsMapper.saveAgvCountTime1(agvTime);
        }
    }

    @Override
    public void deleteStatics(String startTime) {
        int a = statisticsMapper.findAgvYear(startTime);
        if (a != 0) {
            statisticsMapper.deleteStatics(startTime);
        }
    }
}
